from typing import List, Optional
from pydantic import BaseModel, EmailStr, Field, validator
from datetime import datetime

# 用户相关schemas
class UserBase(BaseModel):
    username: str = Field(..., min_length=3, max_length=50)
    email: EmailStr

class UserCreate(UserBase):
    password: str = Field(..., min_length=8)

class UserUpdate(UserBase):
    password: Optional[str] = Field(None, min_length=8)

class User(UserBase):
    id: int
    is_active: bool
    created_at: datetime
    updated_at: datetime
    
    class Config:
        orm_mode = True

# 认证相关schemas
class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: Optional[str] = None

# 简历相关schemas
class PersonalInfoBase(BaseModel):
    full_name: str = Field(..., max_length=100)
    phone: Optional[str] = Field(None, max_length=20)
    email: Optional[EmailStr] = None
    address: Optional[str] = Field(None, max_length=255)
    summary: Optional[str] = None

class PersonalInfoCreate(PersonalInfoBase):
    pass

class PersonalInfoUpdate(PersonalInfoBase):
    pass

class PersonalInfo(PersonalInfoBase):
    id: int
    
    class Config:
        orm_mode = True

# 教育经历相关schemas
class EducationBase(BaseModel):
    institution: str = Field(..., max_length=100)
    degree: str = Field(..., max_length=50)
    major: Optional[str] = Field(None, max_length=100)
    start_date: Optional[str] = Field(None, max_length=20)
    end_date: Optional[str] = Field(None, max_length=20)
    description: Optional[str] = None

class EducationCreate(EducationBase):
    pass

class EducationUpdate(EducationBase):
    pass

class Education(EducationBase):
    id: int
    
    class Config:
        orm_mode = True

# 工作经历相关schemas
class WorkExperienceBase(BaseModel):
    company: str = Field(..., max_length=100)
    position: str = Field(..., max_length=100)
    start_date: Optional[str] = Field(None, max_length=20)
    end_date: Optional[str] = Field(None, max_length=20)
    description: Optional[str] = None
    achievements: Optional[str] = None

class WorkExperienceCreate(WorkExperienceBase):
    pass

class WorkExperienceUpdate(WorkExperienceBase):
    pass

class WorkExperience(WorkExperienceBase):
    id: int
    
    class Config:
        orm_mode = True

# 技能相关schemas
class SkillBase(BaseModel):
    name: str = Field(..., max_length=50)
    level: int = Field(1, ge=1, le=5)

class SkillCreate(SkillBase):
    pass

class SkillUpdate(SkillBase):
    pass

class Skill(SkillBase):
    id: int
    
    class Config:
        orm_mode = True

# 证书相关schemas
class CertificateBase(BaseModel):
    name: str = Field(..., max_length=100)
    issuing_authority: Optional[str] = Field(None, max_length=100)
    issue_date: Optional[str] = Field(None, max_length=20)
    expiry_date: Optional[str] = Field(None, max_length=20)

class CertificateCreate(CertificateBase):
    pass

class CertificateUpdate(CertificateBase):
    pass

class Certificate(CertificateBase):
    id: int
    
    class Config:
        orm_mode = True

# 简历完整schema
class ResumeBase(BaseModel):
    title: str = Field(..., max_length=100)
    template_type: str = Field("default", max_length=50)
    is_active: bool = True

class ResumeCreate(ResumeBase):
    pass

class ResumeUpdate(ResumeBase):
    pass

class Resume(ResumeBase):
    id: int
    user_id: int
    created_at: datetime
    updated_at: datetime
    personal_info: Optional[PersonalInfo] = None
    education: List[Education] = []
    work_experience: List[WorkExperience] = []
    skills: List[Skill] = []
    certificates: List[Certificate] = []
    
    class Config:
        orm_mode = True

# 职位描述相关schemas
class JobDescriptionBase(BaseModel):
    title: str = Field(..., max_length=100)
    company: Optional[str] = Field(None, max_length=100)
    content: str = Field(..., min_length=10)
    industry: Optional[str] = Field(None, max_length=50)
    job_type: Optional[str] = Field(None, max_length=50)
    key_skills: Optional[str] = None

class JobDescriptionCreate(JobDescriptionBase):
    pass

class JobDescription(JobDescriptionBase):
    id: int
    created_at: datetime
    
    class Config:
        orm_mode = True

# AI优化请求schema
class AIOptimizationRequest(BaseModel):
    resume_id: int
    job_description_id: Optional[int] = None
    job_description_text: Optional[str] = None
    optimization_type: str = Field("full", pattern="^(full|experience|skills|summary)$")
    industry_type: Optional[str] = None

# 简历生成请求schema
class ResumeGenerateRequest(BaseModel):
    resume_id: int
    template_type: str = Field("default", max_length=50)
    format: str = Field("pdf", pattern="^(pdf|docx)$")